package com.tzy.for10evalue.controller;

import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;

/**
 * @author tzy
 * @date 2022/11/17 15:10
 */
public class HaoyongThreadController {
    //等待所有任务执行完毕 countdownlatch
    public void thread1() {
        CountDownLatch countDownLatch = new CountDownLatch(items.size());//有多少任务要跑
        for (ZSSDCRMDEVINPUT zssdcrmdevinput : items) {
            syncSapSalePerformanceThreadPool.execute(() -> {
                try {
                    //item 业务处理
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch.countDown();//执行一次数量-1
                }
            });
        }
        try {
            countDownLatch.await();//等待所有任务完成
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //年度折线图异步每月计算
    public void completableFutures() {
        for (int i = 0; i < 12; i++) {//统计12个月
            enddate = localDate.with(TemporalAdjusters.lastDayOfMonth()); // 获取当前月的最后一天
            String starttime = DateUtils.getFormatDayStartStr(localDate);
            String endtime = DateUtils.getFormatDayEndStr(enddate);
            LocalDate finalStartdate = localDate;
            localDate = localDate.plusMonths(1);

            completableFutures.add(CompletableFuture.supplyAsync(() -> {
                clueBoardQO.setStartDatetime(DateUtils.str2Date(starttime, DateUtils.FORMAT_DATETIME_STR));
                clueBoardQO.setEndDatetime(DateUtils.str2Date(endtime, DateUtils.FORMAT_DATETIME_STR));
                List<ResultMap> lastbiddedList = dlClueBaseService.biddedClueGroupByCustomerlevel(clueBoardQO);
                lastbiddedList = filterInvalidByEvalevel(lastbiddedList, clueLineBoardQO.getCustomerlevelList());
                int count = 0;
                ClueBiddedLineChartDTO clueBiddedLineChartDTO = new ClueBiddedLineChartDTO(DateUtils.getFormatDateTimeStr(finalStartdate, DateUtils.FORMAT_DATE_STR), 0, "0%");
                clueBiddedLineChartDTO.setCount(CollectionUtils.isEmpty(lastbiddedList) ? count : lastbiddedList.stream().mapToInt(ResultMap::getCount).sum());
                return clueBiddedLineChartDTO;
            }, threadPoolExecutor));

            completableFutures1.add(CompletableFuture.supplyAsync(() -> {
                clueBoardQO.setStartDatetime(DateUtils.str2Date(starttime, DateUtils.FORMAT_DATETIME_STR));
                clueBoardQO.setEndDatetime(DateUtils.str2Date(endtime, DateUtils.FORMAT_DATETIME_STR));
                List<ResultMap> resultMapList = dlClueBaseService.hasProjectresultClueGroupByCustomerlevel(clueBoardQO);
                resultMapList = filterInvalidByEvalevel(resultMapList, clueLineBoardQO.getCustomerlevelList());
                int count = 0;
                ClueBiddedLineChartDTO clueBiddedLineChartDTO = new ClueBiddedLineChartDTO(DateUtils.getFormatDateTimeStr(finalStartdate, DateUtils.FORMAT_DATE_STR), 0, "0%");
                clueBiddedLineChartDTO.setCount(CollectionUtils.isEmpty(resultMapList) ? count : resultMapList.stream().mapToInt(ResultMap::getCount).sum());
                return clueBiddedLineChartDTO;
            }, threadPoolExecutor));
        }
        // 等待所有任务执行完
        CompletableFuture<Object>[] arr = completableFutures.toArray(new CompletableFuture[12]);
        CompletableFuture.allOf(arr).join();

        CompletableFuture<Object>[] arr1 = completableFutures1.toArray(new CompletableFuture[12]);
        CompletableFuture.allOf(arr1).join();

        for (CompletableFuture<ClueBiddedLineChartDTO> completableFuture : completableFutures) {
            try {
                lineChartDTOList.add(completableFuture.get());//中标数结果
            } catch (Exception e) {
                throw new CommonException("查询失败");
            }
        }
        for (CompletableFuture<ClueBiddedLineChartDTO> completableFuture : completableFutures1) {
            try {
                lineChartDTOList2.add(completableFuture.get());//投标总数结果
            } catch (Exception e) {
                throw new CommonException("查询失败");
            }
        }
    }
}
